package com.luoyuanxiangvip.admin.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
import com.luoyuanxiangvip.common.constant.Constant;
import com.luoyuanxiangvip.common.holder.RequestHolder;
import com.luoyuanxiangvip.common.utils.AjaxResult;
import com.luoyuanxiangvip.common.utils.PageUtil;
import com.luoyuanxiangvip.common.vo.BizArticleVO;
import com.luoyuanxiangvip.core.config.QiniuConfig;
import com.luoyuanxiangvip.core.entity.BizArticle;
import com.luoyuanxiangvip.core.entity.SysUser;
import com.luoyuanxiangvip.core.service.IBizArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019-05-26
 */
@RestController
@RequestMapping("/admin/article")
@Api(description = "文章管理")
public class BizArticleController{

    @Resource
    private QiniuConfig qiniuConfig;

    @Resource
    private IBizArticleService iBizArticleService;

    @RequiresAuthentication
    @ApiOperation(value = "获取文章列表")
    @PostMapping("list")
    public AjaxResult list(@RequestBody BizArticleVO bizArticleVO) {
        IPage<BizArticle> page = iBizArticleService.findPageBreakByCondition(bizArticleVO);
        return AjaxResult.success("获取文章列表成功", new PageUtil<>(page));
    }

    @RequiresAuthentication
    @ApiOperation(value = "添加或修改文章")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody BizArticle article) {
        SysUser user = (SysUser) RequestHolder.getSession().getAttribute(Constant.USER);
        article.setUserId(user.getId());
        return iBizArticleService.insertOrUpdate(article) ? AjaxResult.success("添加或修改文章成功") : AjaxResult.error("添加或修改文章失败");
    }

    @RequiresAuthentication
    @ApiOperation(value = "获取文章详情")
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        BizArticle article = iBizArticleService.info(id, null);
        return AjaxResult.success("获取文章详情成功", article);
    }

    @RequiresAuthentication
    @ApiOperation(value = "删除文章")
    @DeleteMapping("/del/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        return iBizArticleService.removeById(id) ? AjaxResult.success("删除文章成功") : AjaxResult.error("删除文章失败");
    }

    @RequiresAuthentication
    @ApiOperation(value = "修改状态")
    @PatchMapping("/changeStatus/{id}")
    public AjaxResult changeStatus(@PathVariable("id") Long id,
                                   Boolean value,
                                   String type) {
        UpdateChainWrapper<BizArticle> chainWrapper = iBizArticleService.update().set(type, value).eq("id", id);
        boolean update = chainWrapper.update();
        return update ? AjaxResult.success("修改状态成功") : AjaxResult.error("修改状态失败");
    }

    @RequiresAuthentication
    @ApiOperation(value = "文章条数查询")
    @GetMapping("/articleCount")
    public AjaxResult articleCount() {
        int count = iBizArticleService.count();
        return AjaxResult.success("文章条数查询成功", count);
    }

    @RequiresAuthentication
    @ApiOperation(value = "文件上传")
    @PostMapping("/update")
    public Map<String, Object> update(MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>(3);
        String imgUrl = qiniuConfig.upload(file, true);
        map.put("code", 0);
        map.put("message", "图片上传成功");
        map.put("result", imgUrl);
        return map;
    }

    @ApiOperation(value = "获取文件列表")
    @GetMapping("/fileList")
    public AjaxResult fileList() {
        return AjaxResult.success("获取文件列表", qiniuConfig.fileList());
    }

    @ApiOperation(value = "百度推送")
    @PostMapping("/push")
    public AjaxResult push(Long[] ids) {
        return AjaxResult.success("百度推送成功", JSONObject.parseObject(iBizArticleService.push(ids)));
    }
}
